package com.bmps.springcloud.insuredmicroservice.service.calculate;

import com.bmps.springcloud.common.response.TaskResponseModel;
import com.bmps.springcloud.insuredmicroservice.config.accept.ProductConfig;
import com.bmps.springcloud.insuredmicroservice.constant.accept.AcceptConstant;
import com.bmps.springcloud.insuredmicroservice.constant.accept.StrategyConstant;
import com.bmps.springcloud.insuredmicroservice.model.accept.LcPolModel;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * description:
 *
 * @author jackdaw
 * @date 2019-12-25 15:12
 */
@Service
public class AccountValueInsuranceServiceImpl implements AccountValueInsuranceService {
    private Logger logger = LoggerFactory.getLogger(AccountValueInsuranceServiceImpl.class);
    private final AccountValueService accountValueService;
    private final ProductConfig productConfig;

    public AccountValueInsuranceServiceImpl(AccountValueService accountValueService, ProductConfig productConfig) {
        this.accountValueService = accountValueService;
        this.productConfig = productConfig;
    }

    @Override
    public TaskResponseModel accountValue(String key, LcPolModel lcPolModel) {
        TaskResponseModel taskResponseModel = new TaskResponseModel();
        taskResponseModel.setKey(key);
        taskResponseModel.setResultCode(AcceptConstant.RESULT_SUCCESS_CODE);
        String riskType = lcPolModel.getRiskType();
        BigDecimal accountValue = BigDecimal.valueOf(0);

        if (productConfig.getAccountValueList().contains(lcPolModel.getRiskCode())) {
            try {
                if (StringUtils.equals(StrategyConstant.RISK_TYPE_Investment, riskType)) {

                    accountValue = accountValueService.calculateInvestment(lcPolModel.getContNo());

                } else if (StringUtils.equals(StrategyConstant.RISK_TYPE_Universal, riskType)) {
                    accountValue = accountValueService.calculateUniversal(lcPolModel.getContNo());
                }
            } catch (Exception e) {
                logger.error("查询账户价值失败" + lcPolModel.toString(), e);
                taskResponseModel.setResultCode(AcceptConstant.RESULT_FAIL_CODE);
                taskResponseModel.setResultMessage(e.toString());
                accountValue = lcPolModel.getPrem();
            }
            if (accountValue.compareTo(BigDecimal.ZERO) == -1) {
                accountValue = lcPolModel.getPrem();
            }
        }
        taskResponseModel.setBigDecimal(accountValue.setScale(0, BigDecimal.ROUND_HALF_UP));
        return taskResponseModel;
    }
}
